 package com.glory.dgcpcpda.activity.mfg;

import android.os.Handler;
import android.util.SparseArray;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.activity.recipe.ProgramDownLoadActivity;
import com.glory.dgcpcpda.adapter.AddCarrierDataAdapter;
import com.glory.dgcpcpda.adapter.CarrierCheckAdapter;
import com.glory.dgcpcpda.adapter.PackageCheckAdapter;
import com.glory.dgcpcpda.adapter.ElectroplatingRecyclingAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.FTTestProgramListEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.StationDtoEntity;
import com.glory.dgcpcpda.entity.ToolCheckEntity;
import com.glory.dgcpcpda.entity.UpdateIPEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


 public class ElectroplatingRecyclingActivity extends BaseActivity {

     @BindView(R.id.tvTitle)
     TextView tvTitle;
     @BindView(R.id.tvBaseSubmit)
     TextView tvBaseSubmit;
     @BindView(R.id.llMain)
     LinearLayout llMain;
     @BindView(R.id.rvRunLotList)
     RecyclerView rvRunLotList;
     @BindView(R.id.etLot)
     FreeUI_ClearEditText etLot;
     @BindView(R.id.etUserName)
     FreeUI_ClearEditText etUserName;


     private ElectroplatingRecyclingAdapter mRunAdapter;
     private List<String> isScanmDurablelist;

     @Override
     protected int getLayoutResId() {
         return R.layout.activity_electroplating_recycling;
     }

     @Override
     protected void initView() {
         tvTitle.setText("电镀回收");
         tvBaseSubmit.setVisibility(View.GONE);
         setMainLayout(llMain);
         initRecyclerView(rvRunLotList,this);
         mRunAdapter = new ElectroplatingRecyclingAdapter(null);
         mRunAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
//         mRunAdapter.isFirstOnly(true);
         rvRunLotList.setAdapter(mRunAdapter);

         initEdit(etLot,s->requestRunLot(s));


     }

     /**
      * 请求正在运行的批次列表
      * @param lotId
      */
     private void requestRunLot(String lotId) {
         FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                 (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                 }.getType(), getSupportFragmentManager(), this) {

             @Override
             public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                 super.onSuccess(response);
                 if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                     requestLot(lotId);
                 }
             }

             @Override
             public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                 super.onError(response);
                 loadError(response.getException(),"CPC.OUTSOURCE");
             }
         };
         JsonObject map = new JsonObject();
         if (getText(etUserName).equals("")){
             showToast("请输入工号",false);
             return;
         }
         map.addProperty("LOTID",lotId);
         NetUtils.mRequestPostNet("CPC.OUTSOURCE",getText(etUserName),"TRANSFERMES",this,map,callBack);

     }

     private void requestLot(String lotId){
         LotUtils.searchLotInfo(this, lotId, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
             @Override
             public void onSuccess(LotInfoEntity response) {
                 if (!response.getLOT().equals(null)) {
                     mRunAdapter.addData(response.getLOT());
                     etLot.setText("");
                     new Handler().postDelayed(() -> etLot.requestFocus(), 500);
                 }else {
                     showToast("批次信息为空",false);
                     mRunAdapter.setNewData(null);
                 }
             }

             @Override
             public void onFail(Throwable t) {
                 loadError(t, "LOTQUERY");
             }
         });
     }

     @Override
     protected void initData() {

     }


     @OnClick({R.id.tvBaseSubmit})
     public void onViewClicked(View view) {
         switch (view.getId()) {
             case R.id.tvBaseSubmit:
                 break;
         }
     }
 }
